// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.ui.editors;

import java.util.Date;

import org.dengues.commons.IDenguesSharedImage;
import org.dengues.commons.utils.DenguesTextUtil;
import org.dengues.commons.utils.ImageUtil;
import org.dengues.core.resource.WarehouseResourceFactory;
import org.dengues.core.warehouse.ENodeCategoryName;
import org.dengues.core.warehouse.ENodeStatus;
import org.dengues.core.warehouse.IWarehouseNode;
import org.dengues.core.warehouse.IWarehouseView;
import org.dengues.model.warehouse.ScriptsStorage;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.PlatformUI;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-4-25 qiang.zhang $
 * 
 */
public class JavaScriptEditor extends CompilationUnitEditor {

    private ScriptsStorage scriptsStorage;

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor#doSetInput(org.eclipse.ui.IEditorInput)
     */
    @Override
    protected void doSetInput(IEditorInput input) throws CoreException {
        AbstractEditorInput input2 = null;
        if (input instanceof AbstractEditorInput) {
            input2 = (AbstractEditorInput) input;
        } else {
            IFileEditorInput editorInput = (IFileEditorInput) input;
            input2 = new AbstractEditorInput(editorInput.getFile(), editorInput.getName(), ENodeCategoryName.SCRIPTS);
        }
        scriptsStorage = (ScriptsStorage) input2.getEMFStorage();
        super.doSetInput(input2);
        IWarehouseNode warehouseNode = input2.getWarehouseNode();
        if (warehouseNode != null) {
            warehouseNode.setNodeStatus(ENodeStatus.ACTIVED);
            refreshWarehouseView();
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.WorkbenchPart#getPartName()
     */
    @Override
    public String getPartName() {
        if (scriptsStorage != null) {
            return scriptsStorage.getName();
        }
        return super.getPartName();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.EditorPart#getTitleToolTip()
     */
    @Override
    public String getTitleToolTip() {
        if (scriptsStorage != null) {
            return DenguesTextUtil.getNotNullString(scriptsStorage.getComment());
        }
        return super.getTitleToolTip();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.WorkbenchPart#getTitleImage()
     */
    @Override
    public Image getTitleImage() {
        return ImageUtil.getImage(IDenguesSharedImage.PALETTE_CODE);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "refreshWarehouseView".
     */
    protected void refreshWarehouseView() {
        IViewPart findView = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
                .findView(IWarehouseView.VIEW_ID);
        if (findView instanceof IWarehouseView) {
            ((IWarehouseView) findView).refresh(((AbstractEditorInput) getEditorInput()).getWarehouseNode().getParent());
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor#dispose()
     */
    @Override
    public void dispose() {
        if (getEditorInput() instanceof AbstractEditorInput) {
            AbstractEditorInput processEditorInput = ((AbstractEditorInput) getEditorInput());
            IWarehouseNode warehouseNode = processEditorInput.getWarehouseNode();
            if (warehouseNode != null) {
                warehouseNode.setNodeStatus(ENodeStatus.NORMAL);
                refreshWarehouseView();
            }
        }
        super.dispose();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor#doSave(org.eclipse.core.runtime.IProgressMonitor)
     */
    @Override
    public void doSave(IProgressMonitor progressMonitor) {
        super.doSave(progressMonitor);
        scriptsStorage.setModified(new Date());
        scriptsStorage.setContent(getSourceViewer().getTextWidget().getText());
        WarehouseResourceFactory.saveStorage(scriptsStorage);
    }

}
